package com.zhzh.sc.demo.juc.thread;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: dasouche
 * @date: 2021-03-19 14:39
 **/
public class ParallelStreamDemo {

    static List<Integer> nums = new ArrayList<>();
    static {
        Random r = new Random();
        for(int i=0;i<1000;i++){
            nums.add(1000000 + r.nextInt(1000000));
        }
    }

    static void foreach() {
        nums.forEach(v -> isPrime(v));
    }

    static void parallel() {
        nums.parallelStream().forEach(ParallelStreamDemo::isPrime);
    }

    static boolean isPrime(int num) {
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Class<?> aClass = MethodHandles.lookup().lookupClass();
        System.out.println(aClass);
        long start = System.currentTimeMillis();
        foreach();
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        parallel();
        System.out.println(System.currentTimeMillis() - start);
    }
}
